import { _decorator, ProgressBar, Sprite } from 'cc';
import { LayerType } from '../../Basic/Config/BaseEnum';
import { PanelBase } from '../../Basic/UI/PanelBase';
import { TowerItem } from './TowerItem';
import { EventManager } from '../../Basic/Manager/EventManager';
import { GameEventType } from '../Config/GameEnum';
import { autoComp } from '../../Basic/Manager/BaseManager';

const { ccclass, property } = _decorator;
@ccclass('RepairPanel')
export class RepairPanel extends PanelBase {
  panelLayer: LayerType.DialogLayer = LayerType.DialogLayer;
  clickBackClose: boolean = false;
  blackMask: number = 0;
  @autoComp(Sprite)
  door: Sprite;
  @autoComp(ProgressBar)
  door_hp: ProgressBar;
  item: TowerItem;

  onReady(): void {
    EventManager.Ins().register(GameEventType.MAIN_REPAIR, this.refreshUI, this);
    this.door.spriteFrame = this.item.sp.spriteFrame;
    this.refreshUI();
  }

  refreshUI() {
    this.door_hp.progress = this.item.data.hp / this.item.data.hpMax;
  }

  onCloseReady(): void {
    EventManager.Ins().unregister(GameEventType.MAIN_REPAIR, this.refreshUI);
  }

  onClickBtn() {
    this.item.repair();
  }
}
